<template>
  <div>
    <el-card shadow="always" :body-style="{ padding: '20px' }" :style="{ marginBottom: '30px' }">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="一级分类">
          <el-select v-model="category1Id" placeholder="请选择" @change="Category1IdChange">
            <el-option v-for="item in categoryList1" :label="item.name" :value="item.id" :key="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="category2Id" placeholder="请选择" @change="Category2IdChange">
            <el-option v-for="item in categoryList2" :label="item.name" :value="item.id" :key="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="三级分类">
          <el-select v-model="category3Id" placeholder="请选择">
            <el-option v-for="item in categoryList3" :label="item.name" :value="item.id" :key="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>


  </div>
</template>

<script lang="ts">
import useAttrStore from "@/stores/attr";
import { defineComponent } from "vue";

export default defineComponent({
  name: "Category",
});
</script>

<script setup lang="ts">
import { onMounted } from 'vue'
import { storeToRefs } from 'pinia';

//获取attr仓库实例对象
const attrStore = useAttrStore()
//获取attr仓库中的state数据
const { categoryList1, category1Id, categoryList2, category2Id, categoryList3, category3Id } = storeToRefs(attrStore)

onMounted(() => {
  //初始化的时候获取categorylist1的数据
  attrStore.getCategorylist1()
})

//获取二级分类:1.watch category1id的变化   ,2.form表单的@change事件
//1.获取二级分类列表
const Category1IdChange = () => {
  //重新点击一级分类的时候清空二级分类,三级分类
  categoryList2.value = [], category2Id.value = null, categoryList3.value = [], category3Id.value = null
  attrStore.getCategorylist2()
}
//2.获取三级分类列表
const Category2IdChange = () => {
  categoryList3.value = [], category3Id.value = null
  attrStore.getCategorylist3()
}
</script>
<style>
</style>